package com.appkubes.mall.restful.application.goods.goods.dao;

import com.appkubes.mall.core.dao.BaseMallGoodsIndexDao;
import com.appkubes.mall.restful.application.goods.goods.entity.model.GoodsIndexResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GoodsIndexDao extends BaseMallGoodsIndexDao {

    @Select({"SELECT " +
            " b.*, coalesce(c.name, b.name) as name,c.id as details, c.id as extend_id, coalesce(c.cat_id, b.cat_id) as cat_id," +
            " c.sort as extend_index, coalesce(c.price, b.price) as price, coalesce(c.state, b.state) as state," +
            " coalesce(c.is_hot, b.is_hot) as is_hot, coalesce(c.subheading, b.subheading) as subheading " +
            " FROM `mall_goods_index` `a` INNER JOIN `mall_goods` `b` ON `a`.`good_id`=`b`.`id` LEFT JOIN" +
            " `mall_goods_extend` `c` ON `b`.`id`=c.good_id AND c.app_key=#{appKey} WHERE (${queryStr}) ORDER BY coalesce(`c`.`sort`, `b`.`sort`) LIMIT" +
            " #{page},#{limit}"})
    List<GoodsIndexResult> getList(@Param("queryStr") String queryStr,
                                   @Param("appKey") String appKey,
                                   @Param("page") int page,
                                   @Param("limit") int limit);

}